package com.supervision.manage.mapper.sqlprovider

import com.supervision.domain.bean.po.CaseEnforcePO
import org.apache.commons.lang3.StringUtils

/**
 * @ClassName: CaseEnforceSqlProvider
 * @description:
 * @author: whis
 * @create: 2020-05-03 11:22
 */
class CaseEnforceSqlProvider {

    fun getCaseEnforceList(param: HashMap<String, Any>): String {
        val po = param["po"] as CaseEnforcePO
        val limitSql = param["limitSql"] as String

        val sql = StringBuilder()
        if (limitSql.isEmpty()) {
            sql.append(" select count(*) from (select count(ce.id) ")
        } else {
            sql.append(" select ce.*, u.name, u.nickname, u.phone ")
        }
        sql.append(" from case_enforce ce ")
        sql.append(" left join user u on u.id = ce.apply_user_id ")
        sql.append(" left join case_user cu on cu.case_id = ce.id ")
        sql.append(" where ce.weight >= 0 ")
        if (po.sn.isNotEmpty()) {
            sql.append(" and ce.sn like concat('%', #{po.sn} ,'%') ")
        }
        if (po.type > 0) {
            sql.append(" and ce.type = #{po.type} ")
        }
        if (po.applyUser.isNotEmpty()) {
            sql.append(" and u.name like concat('%', #{po.applyUser}, '%') ")
        }
        if (po.defendant.isNotEmpty()) {
            sql.append(" and cu.name like concat('%', #{po.defendant}, '%') ")
        }
        if (limitSql.isEmpty()) {
            sql.append(" group by ce.id ) temp")
        } else {
            sql.append(" group by ce.id ")
            sql.append(" order by ce.id desc ")
        }

        return sql.toString() + StringUtils.defaultString(limitSql, StringUtils.EMPTY)
    }
}